package com.zyfdroid.bigdialerkit;

import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.zyfdroid.bigdialerkit.utils.BaseActivity;

public class NoteActivity extends BaseActivity {

    View pageInputName;
    View pageInputNumber;
    View pageConfirmInput;

    EditText txtInputName;
    EditText txtInputNumber;

    TextView txtConfirmName;
    TextView txtConfirmNumber;

    private void initUI(){
        pageConfirmInput = findViewById(R.id.pageNoteConfirm);
        pageInputName = findViewById(R.id.pageNoteName);
        pageInputNumber = findViewById(R.id.pageNoteNumber);

        txtInputName = findViewById(R.id.txtNoteName);
        txtInputNumber = findViewById(R.id.txtDialerText);
        txtConfirmName = findViewById(R.id.txtNoteConfirmName);
        txtConfirmNumber = findViewById(R.id.txtNoteConfirmPhone);

        gotoPageName();
    }

    /**
     * 显示键盘
     *
     * @param et 输入焦点
     */
    public void showInput(final EditText et) {
        et.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }

    /**
     * 隐藏键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    private void gotoPageName(){
        pageInputName.setVisibility(View.VISIBLE);
        pageInputNumber.setVisibility(View.GONE);
        pageConfirmInput.setVisibility(View.GONE);
        hWnd.postDelayed(new Runnable() {
            @Override
            public void run() {
                txtInputName.requestFocus();
                showInput(txtInputName);
            }
        },200);
    }
    private void gotoPageNumber(){

        pageInputName.setVisibility(View.GONE);
        pageInputNumber.setVisibility(View.VISIBLE);
        pageConfirmInput.setVisibility(View.GONE);
        hideInput();
    }
    private void gotoPageConfirm(){

        pageInputName.setVisibility(View.GONE);
        pageInputNumber.setVisibility(View.GONE);
        pageConfirmInput.setVisibility(View.VISIBLE);
        hideInput();
    }

    Handler hWnd;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        hWnd = new Handler();
        initUI();
    }


    public void noteClearName(View view) {
        txtInputName.setText("");
    }

    public void noteConfirmName(View view) {
        if(TextUtils.isEmpty(txtInputName.getText().toString())){
            showToast("还没输入名字");
            return;
        }
        txtConfirmName.setText(txtInputName.getText().toString());
        gotoPageNumber();
    }
    public void onDialerButtonClick(View view) {
        if(view.getId() == R.id.btnNoteNumberDelete){
            if(txtInputNumber.getText().length() < 1){
                gotoPageName();
                return;
            }
            String oldText = txtInputNumber.getText().toString();
            txtInputNumber.setText(oldText.substring(0,oldText.length() - 1));
        }
        else if(view.getId() == R.id.btnNoteNumberOk){
            if(txtInputNumber.getText().length() < 3){
                showToast("电话号码不完整");
                return;
            }
            txtConfirmNumber.setText(txtInputNumber.getText().toString());
            gotoPageConfirm();
        }
        else{
            if(txtInputNumber.getText().length() >= 18){
                return;
            }
            txtInputNumber.setText(txtInputNumber.getText() + ((Button) view).getText().toString());
        }
    }

    public void noteConfirmAndSave(View view) {
        String name = txtConfirmName.getText().toString();
        String number = txtConfirmNumber.getText().toString();
        try{
            addContact(name,number);
            showToast("添加成功");
            finish();
        }catch (Exception ex){
            ex.printStackTrace();
            showToast("添加失败");
        }
    }

    private void addContact(String name, String telephone) {
        // 创建一个空的ContentValues对象
        ContentValues values = new ContentValues();

        // 向ContactsContract.RawContacts.CONTENT_URI插入一个空的联系人，获取到联系人ID
        Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);

        // 向ContactsContract.Data.CONTENT_URI插入联系人姓名
        values.clear();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

        // 向ContactsContract.Data.CONTENT_URI插入联系人电话号码
        values.clear();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, telephone);
        values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    }

    public void noteCancelAndRestart(View view) {
        gotoPageName();
    }

    @Override
    public void onBackPressed() {
        if(pageConfirmInput.getVisibility() == View.VISIBLE){
            gotoPageNumber();
            return;
        }
        if(pageInputNumber.getVisibility() == View.VISIBLE){
            gotoPageName();
            return;
        }
        super.onBackPressed();
    }


}
